package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
	"strings"
	"xianzhi/service"
	"xianzhi/utils"
)

// 学校相关
type SchoolController struct {
	BaseController
}

// 学校
func (c *SchoolController) Index() {
	userId := c.GetSession("userId")
	key := fmt.Sprintf("selected_%d", userId)
	c.SetSession(key, "")

	p := map[string]interface{}{}
	p["level"] = 2
	_, cityList := service.GetRegionList(p)
	c.Data["cityList"] = cityList

	cityId := c.GetString("city_id")
	c.Data["cityId"] = utils.Atoi64(cityId)
	name := c.GetString("name")
	c.Data["name"] = name

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "school/index.html"
}

// 优选
func (c *SchoolController) Excellent() {
	var selectedNum int
	userId := c.GetSession("userId")
	key := fmt.Sprintf("selected_%d", userId)
	existedSchoolIds := c.GetSession(key)
	if existedSchoolIds == nil || existedSchoolIds == "" {
		selectedNum = 0
	} else {
		existedSchoolIdArr := strings.Split(existedSchoolIds.(string), ",")
		selectedNum = len(existedSchoolIdArr)
	}
	c.Data["selectedNum"] = selectedNum

	id := c.Ctx.Input.Param(":id")
	if len(id) <= 0 {
		c.Redirect(beego.URLFor("controllers.IndexController.Index"), 302)
		return
	}

	p := map[string]interface{}{}
	p["id"] = id
	setMeal := service.GetSetMealInfo(p)
	if setMeal.Id <= 0 {
		c.Redirect(beego.URLFor("controllers.IndexController.Index"), 302)
		return
	}
	c.Data["id"] = setMeal.Id
	c.Data["num"] = setMeal.ExcellentNum

	p = map[string]interface{}{}
	p["level"] = 2
	_, cityList := service.GetRegionList(p)
	c.Data["cityList"] = cityList

	cityId := c.GetString("city_id")
	c.Data["cityId"] = utils.Atoi64(cityId)
	name := c.GetString("name")
	c.Data["name"] = name
	p = map[string]interface{}{}
	p["userId"] = c.GetSession("userId")
	selectedSchoolIds, _ := service.GetSchoolIds(p)
	c.Data["selectedSchoolIds"] = utils.SliceToString(selectedSchoolIds)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "school/excellent.html"
}

// 辅选
func (c *SchoolController) Assist() {
	var selectedNum int
	userId := c.GetSession("userId")
	key := fmt.Sprintf("selected_%d", userId)
	existedSchoolIds := c.GetSession(key)
	if existedSchoolIds == nil || existedSchoolIds == "" {
		selectedNum = 0
	} else {
		existedSchoolIdArr := strings.Split(existedSchoolIds.(string), ",")
		selectedNum = len(existedSchoolIdArr)
	}
	c.Data["selectedNum"] = selectedNum

	id := c.GetString("id")
	if len(id) <= 0 {
		c.Redirect(beego.URLFor("controllers.IndexController.Index"), 302)
		return
	}
	p := map[string]interface{}{}
	p["id"] = id
	setMeal := service.GetSetMealInfo(p)
	if setMeal.Id <= 0 {
		c.Redirect(beego.URLFor("controllers.IndexController.Index"), 302)
		return
	}
	c.Data["maxNum"] = setMeal.ExcellentNum + setMeal.AssistNum
	c.Data["id"] = id
	c.Data["num"] = setMeal.AssistNum
	schoolIds := c.GetString("school_ids")
	excellentSchoolIds := strings.Split(schoolIds, ",")
	if len(excellentSchoolIds) <= 0 {
		c.Redirect(beego.URLFor("controllers.IndexController.Index"), 302)
		return
	}
	if len(excellentSchoolIds) > setMeal.ExcellentNum {
		c.Redirect(beego.URLFor("controllers.IndexController.Index"), 302)
		return
	}
	c.Data["excellentSchoolIds"] = schoolIds
	p = map[string]interface{}{}
	p["level"] = 2
	_, cityList := service.GetRegionList(p)
	c.Data["cityList"] = cityList
	cityId := c.GetString("city_id")
	c.Data["cityId"] = utils.Atoi64(cityId)
	name := c.GetString("name")
	c.Data["name"] = name
	p = map[string]interface{}{}
	p["userId"] = c.GetSession("userId")
	selectedSchoolIds, _ := service.GetSchoolIds(p)
	c.Data["selectedSchoolIds"] = utils.SliceToString(selectedSchoolIds)
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "school/assist.html"
}

func (c *SchoolController) LoadMore() {
	userId := c.GetSession("userId")
	key := fmt.Sprintf("selected_%d", userId)
	selectedSchoolIds := c.GetSession(key)

	count, _ := beego.AppConfig.Int("loadcount")
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	p := map[string]interface{}{}
	p["status"] = 1
	p["selectedSchoolIds"] = selectedSchoolIds
	cityId := c.GetString("city_id")
	if len(cityId) > 0 {
		p["cityId"] = cityId
	}
	name := c.GetString("name")
	if len(name) > 0 {
		p["name"] = name
	}
	isExcellent := c.GetString("is_excellent")
	if len(isExcellent) > 0 {
		p["isExcellent"] = isExcellent
	}
	isAssist := c.GetString("is_assist")
	if len(isAssist) > 0 {
		p["isAssist"] = isAssist
	}
	if len(isExcellent) > 0 || len(isAssist) > 0 {
		c.TplName = "school/load_more_excellent.html"
	} else {
		c.TplName = "school/load_more.html"
	}
	p["curPage"] = curPage
	p["perCount"] = count
	_, schoolList := service.GetSchoolList(p)
	c.Data["schoolList"] = schoolList
}

// 勾选
func (c *SchoolController) DoSelect() {
	var res *utils.ResJsonStruct
	id := c.GetString("id")
	userId := c.GetSession("userId")
	key := fmt.Sprintf("selected_%d", userId)
	ids := c.GetSession(key)
	if ids == nil || ids == "" {
		ids = id
		c.SetSession(key, ids)
	} else {
		ids = fmt.Sprintf("%s,%s", ids, id)
		c.SetSession(key, ids)
	}
	idArr := strings.Split(ids.(string), ",")
	selectedNum := fmt.Sprintf("%d", len(idArr))
	res = utils.ResJson(1, "success", selectedNum)
	c.Data["json"] = res
	c.ServeJSON()
}

// 取消勾选
func (c *SchoolController) CancelSelect() {
	var res *utils.ResJsonStruct
	id := c.GetString("id")
	userId := c.GetSession("userId")
	key := fmt.Sprintf("selected_%d", userId)
	ids := c.GetSession(key)
	idArr := strings.Split(ids.(string), ",")
	for i, sId := range idArr {
		if id == sId {
			idArr = append(idArr[:i], idArr[i+1:]...)
		}
	}
	ids = strings.Join(idArr, ",")
	c.SetSession(key, ids)
	selectedNum := fmt.Sprintf("%d", len(idArr))
	res = utils.ResJson(1, "success", selectedNum)
	c.Data["json"] = res
	c.ServeJSON()
}
